import java.util.Arrays;

public class ArithmeticCoding {
	
	/**
	 * Codes x with arithmetic coding algorithm. Alphabet is "a, b, c ... z"
	 */
	public static boolean[] code(double[] p, char[] x) {
		
		double[] q = Utils.calcCumulatives(p);
				
		char[] sorted = x.clone();
		Arrays.sort(sorted);
		
		double F = 0;
		double G = 1;
		
		for (char c : x) {
			int index = (int) c - (int) 'a';
			F = F + q[index] * G;
			System.out.println(F);
			G = p[index] * G;
		}
		
		System.out.println(F);
		System.out.println(G);
		
		return null;
	}
	
	public static char[] decode(double[] p, boolean[] word) {
		
		return null;
	}
	
	public static void main(String[] args) {
		ArithmeticCoding.code(new double[]{0.4, 0.3, 0.3}, new char[]{'b', 'c', 'a', 'c', 'b', 'a'});
	}
}
